package com.qjdchina.sku.front.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qjdchina.common.web.util.RestBody;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.BrandManager;
import com.qjdchina.sku.biz.UserRoleManager;
import com.qjdchina.sku.consts.WebConsts;
import com.qjdchina.sku.model.vo.BrandVO;
import com.qjdchina.sku.service.enums.SkuResultCode;

/**
 * Created by fankiwu on 2016/12/1.
 */
@Controller
@RequestMapping(value = "brand")
public class BrandController {

    @Autowired
    private BrandManager brandManager;
    @Autowired
    private UserRoleManager userRoleManager;

    @RequestMapping(value = "getBrandByCategory",method = RequestMethod.GET)
    @ResponseBody
    public RestBody<List<BrandVO>> getBrandByCategory(@RequestParam(value = "categoryId", required = true) Integer categoryId,
                                                      HttpServletRequest request) {

        //获取当前登录人的角色
        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute(WebConsts.USER_ID);
        Result<List<Integer>> result = userRoleManager.findByUserId(userId);
        if (!result.isSuccess()) {
            return new RestBody<List<BrandVO>>(result.getCode(), result.getMessage());
        }
        List<Integer> skuRoleIds = result.getDataModel();

        //根据品类和角色获取
        Integer skuCompanyId = (Integer) session.getAttribute(WebConsts.COMPANY_ID);
        Result<List<BrandVO>> brandByCategoryId = brandManager.getBrandByCategoryId(categoryId, skuRoleIds, skuCompanyId);

        if (!brandByCategoryId.isSuccess()) {
            return new RestBody<List<BrandVO>>(brandByCategoryId.getResultCode());
        }

        return new RestBody<List<BrandVO>>(SkuResultCode.SUCCESS, brandByCategoryId.getDataModel());
    }
}
